package com.ruoyi.web.controller.marketing;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.domain.NewUserRegistrationActivity;
import com.ruoyi.system.domain.NewStoreMarketingRule;
import com.ruoyi.system.domain.vo.CardRollDetailsVO;
import com.ruoyi.system.service.INewUserRegistrationActivityService;
import com.ruoyi.system.service.NewStoreMarketingRuleService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity-rule")
@Api(tags = "门店营销活动规则管理")
public class NewStoreMarketingRuleController {


    @Autowired
    private NewStoreMarketingRuleService newStoreMarketingRuleService;

    @Autowired
    private INewUserRegistrationActivityService activityService;


    @PostMapping("/page")
    @ApiOperation("分页查询")
    public BaseResponse<Page<NewStoreMarketingRule>> page(@RequestBody NewStoreMarketingRule rule) {
        LambdaQueryWrapper<NewStoreMarketingRule> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!AbdulEmptyUtils.isEmpty(rule.getActivityName()), NewStoreMarketingRule::getActivityName, rule.getActivityName());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(rule.getActivityCode()), NewStoreMarketingRule::getActivityCode, rule.getActivityCode());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(rule.getStatus()), NewStoreMarketingRule::getStatus, rule.getStatus());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(rule.getMarketingType()), NewStoreMarketingRule::getMarketingType, rule.getMarketingType());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(rule.getStoreId()), NewStoreMarketingRule::getStoreId, rule.getStoreId());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(rule.getPhone()), NewStoreMarketingRule::getPhone, rule.getPhone());
        Page<NewStoreMarketingRule> page = newStoreMarketingRuleService.page(new Page<>(rule.getPageNum(), rule.getPageSize()));
        if (!AbdulEmptyUtils.isEmpty(page.getRecords())) {
            for (NewStoreMarketingRule record : page.getRecords()) {
                String activityCode = record.getActivityCode();
                LambdaQueryWrapper<NewUserRegistrationActivity> wrapper1 = Wrappers.lambdaQuery();
                wrapper1.eq(NewUserRegistrationActivity::getActivityCode, activityCode);
                NewUserRegistrationActivity one = activityService.getOne(wrapper1);
                if (!AbdulEmptyUtils.isEmpty(one)) {
                    record.setActivityName(one.getActivityName());
                    record.setStartDate(one.getStartDate());
                    record.setEndDate(one.getEndDate());
                }
                String cardRollDetails = record.getCardRollDetails();
                if (!AbdulEmptyUtils.isEmpty(cardRollDetails)) {
                    List<CardRollDetailsVO> details = JSON.parseArray(cardRollDetails, CardRollDetailsVO.class);
                    record.setRollDetailsVOList(details);
                }
            }
        }
        return BaseResponse.success(page);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增/修改活动")
    public BaseResponse<Boolean> saveOrUpdate(@RequestBody NewStoreMarketingRule record) {
        validateRecord(record);
        if (record.getRollDetailsVOList() != null && !record.getRollDetailsVOList().isEmpty()) {
            String cardRollDetails = JSON.toJSONString(record.getRollDetailsVOList());
            record.setCardRollDetails(cardRollDetails);
        }
        boolean result = newStoreMarketingRuleService.saveOrUpdate(record);
        return BaseResponse.success(result);
    }

    private void validateRecord(NewStoreMarketingRule record) {
        if (AbdulEmptyUtils.isEmpty(record.getMarketingType())) {
            throw new RuntimeException("请选择活动类型");
        }
        if (record.getMarketingType() == 2) {
            if (AbdulEmptyUtils.isEmpty(record.getCoverImage())) {
                throw new RuntimeException("请上传封面图");
            }
            if (AbdulEmptyUtils.isEmpty(record.getDetailImage())) {
                throw new RuntimeException("请上传详情图");
            }
        }

        if (AbdulEmptyUtils.isEmpty(record.getActivityCode())) {
            throw new RuntimeException("请输入活动编号");
        }
        if (record.getIsGiftCard() != null && record.getIsGiftCard() == 2) {
            if (AbdulEmptyUtils.isEmpty(record.getRollDetailsVOList())) {
                throw new RuntimeException("请填写卡卷详情");
            }
        }
    }


}
